package com.qf.air.search.contorller;

import com.qf.air.search.dto.AirDTO;
import com.qf.air.search.service.ElasticsearchService;
import com.qf.air.search.util.R;
import com.qf.air.search.vo.ResultVO;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author 何星宇
 * @version V1.0
 * @Date 2021/11/8 11:19
 */

@RestController
@RequestMapping("/search")
public class SearchController {
    @Autowired
    private ElasticsearchService elasticsearchService;

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    /**
     * 统计图表
     * @return
     * @throws IOException
     */
    @GetMapping(value = "/echarts")
    public ResultVO echarts() throws IOException {
        List<Map> result = elasticsearchService.echarts();
        return R.ok(result);
    }

    /**
     * 查询空气质量
     * @param airDTO
     * @return
     */
    @PostMapping("/airList")
    public ResultVO list(@RequestBody @Valid AirDTO airDTO){
        return elasticsearchService.findAirIndex(airDTO.getPage()
                , airDTO.getRows()
                , airDTO.getDistrictId()
                , airDTO.getMonitoringStation()
                , airDTO.getStartTime()
                , airDTO.getEndTime());
    }

    /**
     * 添加空气质量信息到ES
     * @param map
     */
    @PostMapping("/add")
    public void add(@RequestBody Map map) throws IOException {
        elasticsearchService.add(map);
    }
}
